package com.pay.ioopos.activity;

import android.content.Intent;
import android.os.Bundle;

import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.R;
import com.pay.ioopos.ScanListener;
import com.pay.ioopos.fragment.PwdInputFragment.OnPwdListener;
import com.pay.ioopos.fragment.RefundIngFragment;
import com.pay.ioopos.fragment.RefundPwdFragment;
import com.pay.ioopos.fragment.RefundScanFragment;
import com.pay.ioopos.fragment.TipVerticalFragment;

import static com.pay.ioopos.KeyInfo.KEY_ENTER;

public class RefundActivity extends AbstractActivity implements ScanListener, OnPwdListener {

    private String pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund);
        RefundPwdFragment fragment = new RefundPwdFragment();
        fragment.setOnPwdListener(this);
        setMainFragment(fragment);
    }

    @Override
    public boolean onKeyDown(KeyInfo keyInfo) {
        if (mainFragment instanceof RefundPwdFragment) {
            RefundPwdFragment fragment = ((RefundPwdFragment)mainFragment);
            fragment.onKeyDown(keyInfo);
        }
        if (keyInfo == KeyInfo.KEY_CANCEL
            || (mainFragment instanceof TipVerticalFragment && keyInfo == KEY_ENTER)) {
            Intent intent = new Intent(this, StatisticsActivity.class);
            startActivity(intent);
        }
        return false;
    }

    private void showRefundScan() {
        RefundScanFragment fragment = new RefundScanFragment();
        fragment.setListener(this);
        setMainFragment(fragment);
    }

    @Override
    public void finish(String pwd) {
        this.pwd = pwd;
        showRefundScan();
    }

    @Override
    public void onScan(String refundNo) {
        setMainFragment(new RefundIngFragment(refundNo, pwd));
    }

}
